﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using ERP_RORZE_DLL.Product.DAO;
using ERP_RORZE_DLL.Product.database;

namespace ERP_RORZE_PRODUCT.Product.WTS
{
    public partial class frm_LaborWTSEdit : DevExpress.XtraEditors.XtraForm
    {
        DataTable dt;
        ClsWTSDAO clsWTSDAO = new ClsWTSDAO();
        ClsLaborWTSTable b = new ClsLaborWTSTable();
        public event EventHandler Reload;

        public string ID;
        public string Date;
        public string Shift;
        public string MachineID;
        public string StaffID;
        public string MONo;
        public string OptionID;
        public string WorkID;
        public string Time;
        public string OKQty;
        public string NGQty;
        public string ProTime;
        public string ClampTime;
        public string Note1;
        public string Note2;

        public frm_LaborWTSEdit()
        {
            InitializeComponent();
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void frm_LaborWTSEdit_Load(object sender, EventArgs e)
        {
            //list machine
            dt = clsWTSDAO.GetAllMachine();
            cbMachine.DataSource = dt;
            cbMachine.DisplayMember = "MachineID";
            cbMachine.ValueMember = "MachineID";

            //list MO Number
            DataTable dt2;
            dt2 = clsWTSDAO.GetAllLWTSMONo();
            DataRow r1 = dt2.NewRow();
            r1["MONo"] = "";
            r1["FullName"] = "";
            dt2.Rows.InsertAt(r1, 0);
            cbMONo.DataSource = dt2;
            cbMONo.DisplayMember = "FullName";
            cbMONo.ValueMember = "MONo";

            

            //list staff
            dt = clsWTSDAO.GetAllStaff();
            cbStaff.DataSource = dt;
            cbStaff.DisplayMember = "StaffID";
            cbStaff.ValueMember = "StaffID";

            //list work
            dt = clsWTSDAO.GetAllWorkLWTS();
            cbWorkID.DataSource = dt;
            cbWorkID.DisplayMember = "WorkID";
            cbWorkID.ValueMember = "WorkID";

            dateDate.Value = DateTime.Parse(Date);
            txtClampTime.Text = ClampTime;
            txtNGQty.Text = NGQty;
            txtNote1.Text = Note1;
            txtNote2.Text = Note2;
            txtOKQty.Text = OKQty;
            txtProTime.Text = ProTime;
            txtTime.Text = Time;
            cbMachine.Text = MachineID;
            cbMONo.SelectedValue = MONo;
            cbOption.Text = OptionID;
            cbShift.Text = Shift;
            cbStaff.SelectedValue = StaffID;
            cbWorkID.SelectedValue = WorkID;
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            try
            {
                int.Parse(txtTime.Text);
                int.Parse(txtClampTime.Text);
                int.Parse(txtNGQty.Text);
                int.Parse(txtOKQty.Text);
                int.Parse(txtProTime.Text);
                //
                b.ID = int.Parse(ID);
                b.Clamptime = float.Parse(txtClampTime.Text);
                b.Date = dateDate.Value;
                b.MachineID = cbMachine.Text;
                b.MONo = cbMONo.SelectedValue.ToString();
                b.NGQty = int.Parse(txtNGQty.Text);
                b.Note1 = txtNote1.Text;
                b.Note2 = txtNote2.Text;
                b.OKQty = int.Parse(txtOKQty.Text);
                b.OptionID = cbOption.Text;
                b.ProTime = float.Parse(txtProTime.Text);
                b.Shift = cbShift.Text;
                b.StaffID = cbStaff.SelectedValue.ToString();
                b.Time = float.Parse(txtTime.Text);
                b.WorkID = cbWorkID.SelectedValue.ToString();
                clsWTSDAO.UpdateLaborWTS(b);
                MessageBox.Show("Đã nhập dữ liệu xong");

                cbWorkID.Text = "";
                txtClampTime.Text = "0";
                txtNote1.Text = "";
                txtNGQty.Text = "0";
                txtNote2.Text = "";
                txtOKQty.Text = "0";
                txtProTime.Text = "0";
                txtTime.Text = "";

                Reload(sender, e);
            }
            
            catch
            {
                MessageBox.Show("Time, ClampTime, NGQty, OKQty, ProTime phải là kiểu số");
            }
        }

        private void cbMONo_SelectedValueChanged(object sender, EventArgs e)
        {
            //list  OptionID
            dt = clsWTSDAO.GetAllOptionID(cbMONo.SelectedValue.ToString());
            cbOption.DataSource = dt;
            cbOption.DisplayMember = "OptionID";
            cbOption.ValueMember = "OptionID";
        }
    }
}